package com.wxb.mybatis.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * describe:
 *
 * @author : 王校兵
 * @version : v1.0
 * @time : 2017-12-03 12:57
 */
@Configuration
@MapperScan(basePackages = "com.wxb.**.dao", sqlSessionFactoryRef = "sqlSessionFactory")
public class DruidConfig {

    @Autowired
    private Environment environment;


    //注入全局数据源属性Map
    @Resource(name = "generalDSPropertyMap")
    private Map<String, Object> dsPropertyMap;

    private ConversionService conversionService = new DefaultConversionService();

    /**
     * 读取数据源配置属性
     * 注册Bean-数据源属性
     *
     * @return
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean(name = "generalDSPropertyMap")
    protected Map<String, Object> generalDSPropertyMap() {
        Map<String, Object> propertyMap = new HashMap<String, Object>();
        return propertyMap;
    }

    @Primary
    @Bean(name = "baseDatasource")
    public DruidDataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
		druidDataSource.setUsername("root");
		druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/boss?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
		druidDataSource.setPassword("123");
        this.dataBinder(druidDataSource);
        return druidDataSource;
    }

    /**
     * 为数据源对象绑定通用配置
     *
     * @param dataSource
     */
    private void dataBinder(DataSource dataSource) {
        if (dsPropertyMap == null || dsPropertyMap.size() < 1) {
            System.out.println("properties is null ================");
        } else {
            RelaxedDataBinder dataBinder = new RelaxedDataBinder(dataSource);

            dataBinder.setConversionService(conversionService);
            dataBinder.setIgnoreNestedProperties(false);
            dataBinder.setIgnoreInvalidFields(false);
            dataBinder.setIgnoreUnknownFields(true);

            dataBinder.bind(new MutablePropertyValues(dsPropertyMap));
        }
    }

    /**
     * sqlSessionFactory
     *
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("baseDatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage(environment.getProperty("mybatis.typeAliasesPackage"));
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(environment.getProperty("mybatis.mapperLocations")));
        return sqlSessionFactoryBean.getObject();
    }

}
